package com.example.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileUtil {
	/**
	 * 删除文件
	 * 
	 * @author xiaozhijun
	 * @Date: 2016年11月9日 下午3:58:45
	 * @param sPath
	 * @return
	 */
	public static boolean deleteFile(String sPath) {
		Boolean flag = false;
		File file = new File(sPath);
		// 路径为文件且不为空则进行删除
		if (file.isFile() && file.exists()) {
			file.delete();
			flag = true;
		}
		return flag;
	}

	/**
	 * 创建新文件
	 * 
	 * @author tcf
	 * @date 2018年5月8日 上午11:53:46
	 * @param filePpath
	 * @return
	 * @throws IOException
	 */
	public static boolean creatNewFile(String filePath) throws IOException {
		boolean flag = false;
		File filename = new File(filePath);
		File fileParent = filename.getParentFile();
		creatNewFolder(fileParent);
		if (filename.exists()) {
			filename.delete();
		}
		filename.createNewFile();
		flag = true;
		return flag;
	}

	/**
	 * 创建文件夹
	 * @param fileParent
	 * @return
	 * @throws IOException
	 */
	public static boolean creatNewFolder(File fileParent) throws IOException {
		boolean flag = false;
		if (!fileParent.exists()) {
			fileParent.mkdirs();
		}
		flag = true;
		return flag;
	}
	/**
	 * 将字符串写入文件
	 * 
	 * @author tcf
	 * @date 2018年5月8日 上午11:53:46
	 * @param filePpath
	 *            文件路径
	 * @param newStr
	 *            写入字段
	 * @param append
	 *            是否追加
	 * @return
	 * @throws IOException
	 */
	public static boolean writeFile(String filePath, String newStr, Boolean append) throws IOException {
		boolean flag = false;
		File filename = new File(filePath);
		if (!filename.exists()) {
			creatNewFile(filePath);
		}
		FileWriter fw = new FileWriter(filePath, append);
		fw.write(newStr);
		fw.close();
		flag = true;
		return flag;
	}
	
	/**
	 * 将文档记录解析成字符串集合
	 * 
	 * @param filePath
	 * @param separator
	 * @throws IOException 
	 */
	public static List readTxt(String filePath) throws IOException {
		File file = new File(filePath);
		List list = new ArrayList();
			FileInputStream fileInputStream = new FileInputStream(file);
			InputStreamReader io = new InputStreamReader(fileInputStream);
			BufferedReader reader = new BufferedReader(io);
			String terminalInfo = null;
			while ((terminalInfo = reader.readLine()) != null) {
				list.add(terminalInfo);
			}
			reader.close();
			io.close();
			fileInputStream.close();
		return list;
	}
	
	 /**
     * 递归删除目录下的所有文件及子目录下所有文件
     * @param dir 将要删除的文件目录
     * @return boolean Returns "true" if all deletions were successful.
     *                 If a deletion fails, the method stops attempting to
     *                 delete and returns "false".
     */
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }
	public static String [] getFileName(String path)
    {
        File file = new File(path);
        String [] fileName = file.list();
        return fileName;
    }
	/**
	 * 获取文件名
	 * @param path
	 * @param fileName
	 */
    public static ArrayList<String> getAllFileName(String path){
    	ArrayList<String> fileName =  new ArrayList<String>();
        File file = new File(path);
        File [] files = file.listFiles();
        String [] names = file.list();
        if(names != null)
        fileName.addAll(Arrays.asList(names));
        for(File a:files)
        {
            if(a.isDirectory())
            {
                getAllFileName(a.getAbsolutePath());
            }
        }
        return fileName;
    }
    
    /**
     * 复制文件
     * @param filePath 被复制的文件路径
     * @param newPath 新的文件路径
     * @param append 是否追加
     * @throws IOException
     */
    public static void copyFile(String filePath ,String newPath ,Boolean append) throws IOException{  
        
        FileWriter fw=new FileWriter(newPath,append);  
        FileReader fr=new FileReader(filePath);  
          
        int ch;  
        while((ch=fr.read())!=-1){  
            fw.write(ch);  
              
        }  
        fw.close();  
        fr.close();  
    }  
    
    /**
     * 读取文件行数
     * 
     * @param path
     *            文件路径
     * @return long
     * @throws IOException 
     */
	public static Integer  readLine(String path) throws IOException {
		Integer index = 0;
		RandomAccessFile file = new RandomAccessFile(new File(path), "r");
		while (null != file.readLine()) {
			index++;
		}
		file.close();
		return index;
	}

	 public static void main(String[] args) throws IOException {
		// ArrayList<String> fileName = new ArrayList<String>();
		// fileName =  FileUtil.getAllFileName("/Users/xiaojun/Desktop/wlw_2/yct");
		/*List list = FileUtil.readTxt("/Users/xiaojun/Desktop/wlw/0001/output/XF8027000120111028/RZ8027000120111028.TXT","\r\n");
		for(int i = 0 ; i< list.size() ; i++){
			String[] str = (String[]) list.get(i);
			String s =org.apache.commons.lang3.StringUtils.join(str);
			System.out.println(s);
		}*/
//		for(int i =0;i<fileName.size();i++){
//			System.out.println(fileName.get(i));
//		}
		
		 System.out.println( FileUtil.readLine("/Users/xiaojun/Desktop/wlw_2/yct/input/0001/cpu/JY000100042018051509.txt"));
		
	}

	 public static void writeHead(File file,byte[] content){
	       
	        try (RandomAccessFile raw = new RandomAccessFile(file,"rw")){
	        	
	        	int srcLength = (int)raw.length() ;
	    		byte[] buff = new byte[srcLength];
	    			raw.read(buff , 0, srcLength);
	    			raw.seek(0);
	    			raw.write(content);
	    			raw.seek(content.length);
	    			raw.write(buff);
	    			raw.close();

	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	 
	 public static void writeEnd(File file,byte[] content){
	        
	        try (RandomAccessFile raw = new RandomAccessFile(file,"rw")){
	            
	            raw.seek(raw.length());
	            raw.write(content);
	            raw.close();
	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }


	
}
